package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.BomBean;
import com.njitt.bysj.bean.ProjectBean;
import com.njitt.bysj.mapper.BomBeanMapper;
import com.njitt.bysj.mapper.ProjectBeanMapper;
import com.njitt.bysj.model.BomModel;
import com.njitt.bysj.query.ProjectProductionQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/20 17:17
 */
@Service
public class ProjectService extends BaseService<ProjectBean, Integer> {

    @Resource
    private ProjectBeanMapper projectBeanMapper;

    @Resource
    private BomBeanMapper bomBeanMapper;

    public Map<String, Object> selectByParamsWithProject(ProjectProductionQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>(16);
        PageInfo<ProjectBean> info = new PageInfo<>(projectBeanMapper.selectByParamsWithProject(query));
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        map.put("code", 0);
        return map;
    }

    public Integer insertBatchList(List<BomModel> list) {
        list.forEach(m -> {
            String projectNo = list.get(0).getProjectNo();
            ProjectBean projectBean = projectBeanMapper.selectProjectByProjectNo(projectNo);
            AssertUtil.isTrue(projectBean == null, "请核实项目号");
            m.setProjectId(projectBean.getId());
        });

        return bomBeanMapper.insertBatchList(list);
    }

    public BomBean selectByPrimaryBom(Integer id) {
        AssertUtil.isTrue(id == null || id == 0, "查询失败");
        BomBean bean = bomBeanMapper.selectByPrimaryKey(id);
        return bean;
    }

    public ProjectBean selectProjectByProjectId(Integer projectId) {
        AssertUtil.isTrue(projectId == null || projectId == 0, "关联项目不存在");
        ProjectBean projectBean = projectBeanMapper.selectByPrimaryKey(projectId);
        AssertUtil.isTrue(projectBean == null, "关联项目不存在");
        return projectBean;
    }

    public ProjectBean selectProjectIdByProjectNo(String projectNo) {
        AssertUtil.isTrue(StringUtils.isBlank(projectNo), "项目号不能为空");
        ProjectBean projectBean = projectBeanMapper.selectProjectByProjectNo(projectNo);
        AssertUtil.isTrue(projectBean == null || projectBean.getId() == null, "项目不存在");
        return projectBean;
    }




}
